package com.admin.kotlintest.activity.setting

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.SaveListener
import com.admin.kotlintest.R
import com.admin.kotlintest.base.BaseActivity
import com.admin.kotlintest.adapter.SpinnerTypeAdapter
import com.admin.kotlintest.bean.MyUser
import com.admin.kotlintest.bean.Type
import com.admin.kotlintest.bean.UserType
import com.admin.kotlintest.config.FixedValue
import com.admin.kotlintest.utils.DataUtil
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_add_user.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/4/12.
 */
class AddUserActivity: BaseActivity(){

    var adapter:SpinnerTypeAdapter?=null
    var type:Type?=null
    var myuser=MyUser()

    override fun bindLayout(): Int {
        return R.layout.activity_add_user
    }

    override fun initData() {
        tv_title.text="添加用户"
        adapter= SpinnerTypeAdapter(context,R.layout.layout_spinner_type_item,null)
        sp_type.adapter=adapter
        getTypes()
    }

    override fun initEvent() {
        iv_title_left.setOnClickListener {
            finish()
        }

        sp_type.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                type=adapter!!.getItem(position)
            }
        }
        btn_save_user.setOnClickListener {
            if(edt_user_name.text == null ||edt_user_name.text.toString()==""){
                toast("请输入用户名")
                return@setOnClickListener
            }
            myuser.username= edt_user_name.text.toString()
            if(edt_password.text==null ||edt_password.text.toString()==""){
                toast("请输入密码")
                return@setOnClickListener
            }
            myuser.setPassword(edt_password.text.toString())
            if(type==null){
                toast("请选择用户类型")
                return@setOnClickListener
            }
            if(edt_email.text!!.toString()!="") {
                myuser.email = edt_email.text!!.toString()
            }
            if(edt_phone.text!!.toString()!=""){
                if(edt_phone.text.toString().length!=11
                        ||!DataUtil.isStringFoematRight(edt_phone.text.toString(), FixedValue.PHONE_STANDARD)){
                    toast("手机号码必须是11位的数字")
                    return@setOnClickListener
                }
                myuser.mobilePhoneNumber=edt_phone.text!!.toString()
            }
            myuser.QQ=edt_qq.text!!.toString()
            myuser.signUp(object :SaveListener<MyUser>(){
                override fun done(p0: MyUser?, p1: BmobException?) {
                    if(p1==null){
                        var userType=UserType(p0,type)
                        userType.save(object : SaveListener<String>() {
                            override fun done(p0: String?, p1: BmobException?) {
                                p1?:let {
                                    toast("用户添加成功")
                                    finish()
                                }
                                p1?.let {
                                    toast("用户添加失败")
                                    KLog.d(p1.message)
                                }
                            }
                        })
                    }else{
                        toast("用户添加失败")
                        KLog.d(p1.message)
                    }
                }
            })
        }
    }

    /**
     * 获取用户所有的类型列表
     */
    fun getTypes(){
        var query=BmobQuery<Type>()
        query.findObjects(object : FindListener<Type>(){
            override fun done(p0: MutableList<Type>?, p1: BmobException?) {
                if(p1==null) {
                    adapter!!.upData(p0!!)
                }
            }
        })
    }

}